const express = require('express')
const router = express.Router()
const userDb = require('../db/user-db')
const {logI} = require('../log.js')
const {retError, retSuccess} = require('../utils/ret')
const checkType = require('../utils/checkType')
const checkToken = require('../utils/checkToken')
const checkDeviceNo = require('../utils/checkDeviceNo')
const { v4: uuidv4} = require('uuid');
const moment = require('moment')

router
    .post('/', async(req, res) => {
        let {body: {maddr,deviceNo,token,buildingUUID}} = req
        logI("A-S", maddr, "QR_receiver_list", `${JSON.stringify(req.body)}`, "null")
        try{
            const error = checkType.mesh(req.body)
            if(error){
                retError("QR_receiver_list",maddr, 10001,  error.details[0].message, res);
            }else{
                let msg = {};
                msg = await checkToken(token,maddr);
                if(msg.code === 0){
                    const deviceInfo={
                        'maddr':maddr,
                        'deviceNo':deviceNo
                    }
                    let device={};
                    device = await checkDeviceNo(deviceInfo);
                    if(device.code === 0){
                        let receiverList = {};
                        receiverList = await userDb.receiverList(maddr,buildingUUID);
                        let msgRet=[]
                        if(receiverList){
                            retSuccess("QR_receiver_list", maddr,0, receiverList, res);
                        }else{
                            retSuccess("QR_receiver_list", maddr,0, msgRet, res);
                        }
                    }else{
                        retError("QR_receiver_list", maddr,device.code, device.body, res);
                    }
                }else{
                    retError("QR_receiver_list", maddr,msg.code, msg.body, res);
                }
            }
        } catch (e) {
            retError("QR_receiver_list",maddr, 10001, e, res);
        }
    })
module.exports = router
